﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace uart_debugger
{
    public partial class Form1 : Form
    {
        string rcvString;
        List<protokoll> sndList = new List<protokoll>();
        List<protokoll> rcvList = new List<protokoll>();

        public Form1()
        {
            InitializeComponent();
            rcvString = "";
        }

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            rcvString += serialPort.ReadExisting();
            //MessageBox.Show(rcvString);
            protokoll pr = new protokoll();
            if (!pr.decodeRcvString(rcvString)) MessageBox.Show("Fehler beim Dekodieren.");
            else
            {
                rcvString = "";
                rcvList.Add(pr);
            }

        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            protokoll pr = new protokoll(rb_ascii.Checked, cb_checksum.Checked, cb_funktion.SelectedIndex + 1, txt_adresse.Text.ToUpper(), txt_data.Text.Length, txt_data.Text);
            sndList.Add(pr);
            //MessageBox.Show(pr.getUartString());
            txt_serialOutput.Text = pr.getUartString();
            serialPort.Write(pr.getUartString());
        }

// Oberfläche -----------------------------------------------------------------------

        private void btn_rcv_Click(object sender, EventArgs e)
        {
            rcvString += txt_rcv.Text;
            txt_rcv.Text = "";
            //MessageBox.Show(rcvString);
            protokoll pr = new protokoll();
            if (!pr.decodeRcvString(rcvString)) MessageBox.Show("Fehler beim Dekodieren.");
            else
            {
                rcvString = "";
                rcvList.Add(pr);
            }
        }

        private void ansichtAktualisieren()
        {
            lb_snd.Items.Clear();
            foreach (protokoll pr1 in sndList)
            {
                lb_snd.Items.Insert(0, pr1.ToString());
            }

            lb_rcv.Items.Clear();
            foreach (protokoll pr2 in rcvList)
            {
                lb_rcv.Items.Insert(0, pr2.ToString());
            }
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort.Open();
                btn_connect.Enabled = false;
                nc_comPort.Enabled = false;
                gb_funktion.Enabled = true;
                btn_disconnect.Enabled = true;
                gb_hwSim.Enabled = true;
                gb_serialOutput.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_disconnect_Click(object sender, EventArgs e)
        {
            serialPort.Close();
            btn_connect.Enabled = true;
            nc_comPort.Enabled = true;
            gb_funktion.Enabled = false;
            btn_disconnect.Enabled = false;
            gb_hwSim.Enabled = false;
            gb_serialOutput.Enabled = false;
        }

        private void nc_comPort_ValueChanged(object sender, EventArgs e)
        {
            serialPort.PortName = "COM" + nc_comPort.Value;
        }

        // StingOperationen ---------------------------------------
        private string hexToString(string hex)
        {
            return Convert.ToString((char)Convert.ToInt32("0x" + hex, 16));
        }

        private void btn_aktualisieren_Click(object sender, EventArgs e)
        {
            ansichtAktualisieren();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
